//
//  MembersController.m
//  MBObjectServer
//
//  Created by Juvid on 14-4-2.
//  Copyright (c) 2014年 MBpayer. All rights reserved.
//

#import "MembersController.h"

@implementation MembersController


-(id)init{
    self=[super init];
    if (!self) {
        return nil;
    }
    baseserver = [[MBBaseServer alloc]init];
    return self;
}
-(void)CancleRequest{
    baseserver.baseclient=nil;
}
-(void)dealloc{
    baseserver.baseclient=nil;
    baseserver=nil;
}
#pragma mark 会员发卡
-(void)BeVIPUser:(NSString *)cardNo
          Mobile:(NSString *)mobile
         Success:(ReceiveSuccess)processSuccess
         Failure:(ReceiveFailure)processFailure{

    NSMutableDictionary* postDic=[[NSMutableDictionary alloc]init];
    [postDic setObject:cardNo forKey:@"CardNo"];
    [postDic setObject:mobile forKey:@"Mobile"];
    [postDic setObject: [DicRequest GetOrgCode] forKey:@"OrgCode"];
    [postDic setObject:[DicRequest GetUserID] forKey:@"SellerID"];
    [postDic setObject:[DicRequest GetUserName] forKey:@"SellerName"];
    [baseserver setRequestStyle:nil andSetPathString:@"vipuser_handler.ashx" andSetHeadString:nil andActionName:@"BeVIPUser" andSetMainDictionary:postDic block:^(NSDictionary *responseData) {
        BOOL  Result = [[responseData objectForKey:@"Result"] boolValue];
        if (Result) {
            [DicRequest ShowAlert:@"成功" Message:[responseData objectForKey:@"Message"]];
        }
        else{
            [DicRequest ShowAlert:@"失败" Message:[responseData objectForKey:@"ErrMessage"]];
        }
        processSuccess(nil);
        
    } failblock:^(NSError *error) {
        processFailure(error);
    } ];

}
#pragma mark 会员查询
-(void)QueryVIPUserByMobileAndCard:(NSString *)number
                           Success:(ReceiveSuccess)processSuccess
                           Failure:(ReceiveFailure)processFailure{
     __block MembersController *selfCtr=self;
    NSMutableDictionary* postDic=[[NSMutableDictionary alloc]init];
    if (number.length == 11) {
        [postDic setObject:number forKey:@"Mobile"];
    }else{
        [postDic setObject:number forKey:@"CardNo"];
    }
        
     [baseserver setRequestStyle:nil andSetPathString:@"vipuser_handler.ashx" andSetHeadString:nil andActionName:@"QueryVIPUserByMobileAndCard" andSetMainDictionary:postDic
                           block:^(NSDictionary *responseData) {
                               [selfCtr QueryVIPResult:responseData Success:processSuccess];
                           } failblock:^(NSError *error) {
                               processFailure(error);
                           }];
}
-(void)QueryVIPResult:(NSDictionary *)responseData Success:(ReceiveSuccess)processSuccess{
    NSMutableDictionary *mlDicReturn;
    BOOL  Result = [[responseData objectForKey:@"Result"] boolValue];
    if (Result) {
        NSString * messageStr =[responseData objectForKey:@"Message"];
        if ( messageStr != nil){
            mlDicReturn=[[NSMutableDictionary alloc]init];
            NSMutableArray * messageMutableArr = [[NSMutableArray alloc]init];
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:[responseData objectForKey:kVIPCode] forKey:kVIPCode];
            NSArray * messageArr = [messageStr componentsSeparatedByString:@";"];
            for (int i = 1; i < messageArr.count ; i ++ )
            {
                
                NSArray * detailMessArr = [[messageArr objectAtIndex:i] componentsSeparatedByString:@":"] ;
                
                if (detailMessArr.count == 2  )
                {
                    [messageMutableArr addObject:[detailMessArr objectAtIndex:1] ];
                }else
                {
                    [messageMutableArr addObject:@" "] ;
                }
                
            }
            [mlDicReturn setValue:messageMutableArr forKey:@"Message"];
           
        }
        
    }
    else{
        [DicRequest ShowAlert:@"失败" Message:[responseData objectForKey:@"ErrMessage"]];
    }
    processSuccess(mlDicReturn);


}
@end
